<?php
/*
 * 投诉静态类
 */
class Feedback{
	/**
	 * 发表投诉
	 * @param <int> $uin
	 * @param <string> $content
	 */
	static public function add($uin , $content){
		$Db = loadDB();
		
		$time = time();
		//把数据写入数组
		$insert_date = array(
			'uin'=>$uin,
			'content'=>$content,
			'reply'=>'',
			'time'=>$time,
		);
		$success = $Db->doInsert('Feedback' , $insert_date);
		return $success ? 0 : 501;
	}
	/**
	 * 获取总投诉数
	 * @param <int> $uin
	 */
	static public function getCount() {
		$Db = loadDB();
		
		$result= $Db->getOne("SELECT COUNT(*) AS `count` From `feedback`");

		return empty($result) ? 0 : $result['count'];
	}
	
	/**
	 * 获取投诉列表
	 * @param <int> $pageSize
	 * @param <int> $pageCount
	 */
	static public function getList($pageSize, $pageCount) {
		$Db = loadDB();
		//$pageSize=1;
		//$pageCount=10;
		$pageSize=($pageSize-1)*$pageCount;
		$list = $Db->getAll("SELECT  * FROM `feedback` ORDER BY id DESC LIMIT {$pageSize} , {$pageCount}");
		
		//解析回复
		foreach($list as $k => $row) {
			$list[$k]['reply'] = empty($row['reply']) ? array() : unserialize($row['reply']);
		}
		
		return $list;
	}
	/**
	 * 获取单条投诉
	 * @param <int> $id
	 * 
	 */
	static public function getSend($id) {
		$Db = loadDB();
		
		$list = $Db->getOne("SELECT  * FROM `feedback` WHERE feedback.id={$id}");
		
		//解析单条回复
		$list['reply'] = empty($list['reply']) ? array() : unserialize($list['reply']);

		return $list;
	}
	/**
	 * 快速回复投诉
	 * @param <int> $uin
	 * @param <int> $id
	 * @param <string> $content
	 */
	static public function getUpSend($uin , $id , $content,$org_feedback = false) {
		$Db = loadDB();
		
		// 取原数据
		if(empty($org_feedback)) {
			$feedback = self::getOne($id);
			if(empty($Feedback)) {
				return 504;	// 该投诉信息不存在
			}
		} else {
			$feedback = $org_feedback;
		}
		
		$time = time();
		$feedback['reply'][] = array('uin' => $uin , 'content' => $content , 'time' => $time);
		
		// 写入数据
		$update_data = array(
			'reply' => serialize($feedback['reply']),
		);
		$success = $Db->doUpdate('feedback' , $update_data , '`id` = ' . $id);

		return $success ? 0 : 502;
	}
	/**
	 * 删除投诉
	 * @param <int> $id
	 */
	static public function delete($id){
		$Db=loadDB();
		
		//删除投诉信息
		$success =$Db->query("DELETE FROM `feedback` WHERE `id`={$id}");
		return $success ? 0 : 503;
	}
}


# end of script
